package com.example.unisys.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.Ip;
import com.example.unisys.service.IpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/apis/v1/ips", produces = "application/json; charset=utf-8")
public class IpController {

    @Autowired
    IpService ipService;

/*
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public HashMap<String, Object> getIPs(@RequestParam("limit") int limit, @RequestParam("page") int page,@RequestParam("main_school") String main_school,@RequestParam("sub_school") String sub_school) {
        HashMap<String, Object> res = new HashMap<>();
        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        List<Ip> ls = ipService.getIps(newlimit, pagesize,main_school,sub_school);
        int sum = ipService.getIpSum();
        res.put("code", 20000);
        res.put("data", ls);
        res.put("total", sum);
        return res;
    }
*/

    /*
     * 添加IP
     * 根据用户
     * */
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public Response createIp(@RequestBody JSONObject jsonParam) {
        Ip ip = JSONObject.parseObject(jsonParam.toJSONString(), Ip.class);
        if (ipService.insert(ip) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*    @RequestMapping(value = "/{ip_id}", method = {RequestMethod.DELETE})
        public HashMap<String, Object> removeById(@PathVariable("ip_id") int ip_id) {
            HashMap<String, Object> res = new HashMap<>();
            if (ipService.delete(ip_id) == 1) {
                res.put("code", 20000);
            } else {
                res.put("code", 400);
            }
            return res;
        }*/
    /*
     * 禁用IP
     * */
    @RequestMapping(value = "/{ip_id}", method = {RequestMethod.DELETE})
    public Response forbidById(@PathVariable("ip_id") int ip_id) {
        if (ipService.forbidIP(ip_id) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
     *  启动IP
     * */
    @RequestMapping(value = "/{ip_id}", method = {RequestMethod.PUT})
    public Response startById(@PathVariable("ip_id") int ip_id) {
        if (ipService.startIP(ip_id) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();

        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
     * 根据学校请求对应IP控制
     * */
    @RequestMapping(value = "/school", method = {RequestMethod.GET})
    public Response getBySchool(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("main_school") String main_school, @RequestParam("sub_school") String sub_school) {
        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        HashMap<String, Object> hs1 = new HashMap<>();
        HashMap<String, Object> hs2 = new HashMap<>();
        if (main_school.equals(sub_school)) {
            hs1.put("main_school", main_school);
            hs1.put("start", newlimit);
            hs1.put("pagesize", pagesize);
            System.out.println("start");
            List<Ip> ips = ipService.getBySchool(hs1);
            hs2.put("main_school", main_school);
            int sum = ipService.getBySchoolSum(hs2);
            return Response.builder().data(ips).total(sum).status(STATUS.SUCCESS).build();
        } else {
            hs1.put("main_school", main_school);
            hs1.put("sub_school", sub_school);
            hs1.put("start", newlimit);
            hs1.put("pagesize", pagesize);
            List<Ip> ips = ipService.getBySchool(hs1);
            hs2.put("main_school", main_school);
            hs2.put("sub_school", sub_school);
            int sum = ipService.getBySchoolSum(hs2);
            return Response.builder().status(STATUS.SUCCESS).total(sum).data(ips).build();
        }
    }


}
